package com.itheima.web;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.LoginData;
import com.itheima.pojo.User;
import com.itheima.service.LoginDataService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    private LoginDataService service = new LoginDataService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //2. 调用MyBatis完成查询
        //2.1 获取SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        //2.2 获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //2.3 获取Mapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //2.4 调用方法
        User user = userMapper.select(username, password);
        //2.5 释放资源
        sqlSession.close();

        //3. 判断user释放为null
        if (user != null) {
            // 登录成功
            HttpSession session = request.getSession();
            session.setAttribute("user", user);

            Date loginTime = new Date();
            String ip = request.getRemoteAddr();

            LoginData loginData = new LoginData();
            loginData.setUserId(user.getId());
            loginData.setLoginTime(loginTime);
            loginData.setIp(ip);

            Integer loginDataId = service.addLogin(loginData);

//            // test
//            System.out.println(loginDataId);
//            System.out.println(loginDataId);
//            System.out.println(loginDataId);
//            System.out.println(loginDataId);
//            System.out.println(loginDataId);

            // 向session中存入loginDataId属性
            session.setAttribute("loginDataId", loginDataId);

//            // test
//            Integer test = (Integer) session.getAttribute("loginDataId");
//            System.out.println(test);
//            System.out.println(test);
//            System.out.println(test);
//            System.out.println(test);
//            System.out.println(test);

            response.sendRedirect("selectAllServlet");
        } else {
            // 登录失败
            response.sendRedirect("login-failed.html");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
